/**
 * 
 */
package com.hand.cloud.b2b.daos.impl;

import de.hybris.platform.core.model.product.ProductModel;
import de.hybris.platform.europe1.model.PriceRowModel;
import de.hybris.platform.servicelayer.exceptions.ModelSavingException;
import de.hybris.platform.servicelayer.model.ModelService;
import de.hybris.platform.servicelayer.search.FlexibleSearchService;

import java.util.Locale;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.hand.cloud.b2b.daos.EPProductDAO;
import com.hand.cloud.b2b.daos.PriceRowDAO;
import com.hand.cloud.b2b.enums.EPProductApproveStatus;
import com.hand.cloud.b2b.enums.EPProductStuffStatus;
import com.hand.cloud.b2b.enums.EPProductType;
import com.hand.cloud.b2b.enums.EPType;
import com.hand.cloud.b2b.model.EPProductModel;


/**
 * @author StephenWang
 * @time Aug 7, 2014 10:52:51 AM
 * 
 */
@Component(value = "epProductDAO")
public class DefaultEPProductDAO implements EPProductDAO
{

	public static Logger LOG = Logger.getLogger(DefaultEPProductDAO.class);

	@Autowired
	private ModelService modelService;

	@Autowired
	private FlexibleSearchService flexibleSearchService;
	
	@Resource
	private PriceRowDAO priceRowDAO;

	/**
	 * 通过Hybris商品信息创建EP商品信息
	 * 
	 * @param product
	 * @return
	 */
	@Override
	public EPProductModel createEPProduct(ProductModel product)
	{

		LOG.info("product.getCode():" + product.getCode());
		LOG.info("product.getDescription():" + product.getDescription(Locale.CHINA));
		LOG.info("product.getName():" + product.getName(Locale.CHINA));
		LOG.info("product.getPk():" + product.getPk());//8796093054977

		//通过查询语句获取商品价格
		PriceRowModel priceRow =priceRowDAO.getPriceRow("USD","8796093054977");
		LOG.info("querystr:" + priceRow.getPrice());


		EPProductModel epProduct = this.modelService.create(EPProductModel.class);
		 
		
		epProduct.setEpType(EPType.TAOBAO);
		//epProduct.setSourcePk("8796093054977");//外部系统的主键
		epProduct.setSourceProduct(product);
		epProduct.setCode(product.getCode());
	   epProduct.setPrice(priceRow.getPrice());
		epProduct.setPrice(new Double(100));
		epProduct.setType(EPProductType.FIXED);
		epProduct.setStuffStatus(EPProductStuffStatus.NEW);
		epProduct.setTitle("hybris测试");
		epProduct.setDesc("hello..");
		epProduct.setLocationState("上海");
		epProduct.setLocationCity("上海");
		epProduct.setApproveStatus(EPProductApproveStatus.ONSALE);
		epProduct.setCid(new Long(50067207));//电子凭证，商品子类目


		try
		{
			this.modelService.save(epProduct);
			LOG.info("----------------epProduct.getPk: " + epProduct.getPk());
		}
		catch (ModelSavingException e)
		{
			LOG.info("---------createEPProduct error-----------");
			e.printStackTrace();
			return null;
		}

		//		final EPProductModel product2 = this.modelService.create(EPProductModel.class);
		//
		//		product2.setNum(333399L);//数量
		//		product2.setPrice(28880d);//价格
		//		product2.setType(EPProductType.FIXED);//一口价，非拍卖
		//		product2.setStuffStatus(EPProductStuffStatus.NEW);//全新，非二手
		//		product2.setTitle("Stephen测试_hybris服务");
		//		product2.setDesc("最好的hybrisRRRRRRRRR");
		//		product2.setLocationCity("上海");
		//		product2.setLocationState("上海");
		//		product2.setCid(50067207L);//电子凭证，商品子类目
		//		product2.setSourcePk("6827348249");//外部系统的主键
		//		product2.setEpType(EPType.TAOBAO);
		//
		//		try
		//		{
		//			this.modelService.save(product2);
		//		}
		//		catch (ModelSavingException e)
		//		{
		//			LOG.info("---------createEPProduct22222 error-----------");
		//			e.printStackTrace();
		//			return null;
		//		}


		return epProduct;
	}

	/**
	 * 通过Hybris商品信息更新EP商品信息
	 * 
	 */
	@Override
	public EPProductModel updateEPProduct(ProductModel product)
	{
	   EPProductModel epProduct = new EPProductModel();
	   epProduct.setSourceProduct(product);
		
		try
		{
			epProduct = this.flexibleSearchService.getModelByExample(epProduct);
			LOG.info("-----EpProdcutCode:" + epProduct.getCode());
			epProduct.setNum(new Long(100));
			epProduct.setType(EPProductType.FIXED);
			epProduct.setPrice(new Double(100.0));
			epProduct.setStuffStatus(EPProductStuffStatus.NEW);
			epProduct.setTitle("Hybris");
			epProduct.setDesc("hello..");
			epProduct.setLocationState("上海");
			epProduct.setLocationCity("上海");
			epProduct.setCid(new Long(50067207));
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}	
		
		return epProduct;
	}

}
